package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.CompleteActivityInfoTask;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.R;

/**
 * Created by XCKJ_Paladowow on 2016/6/17.
 */
public class QiangYueFanActivity extends BaseActivity implements View.OnClickListener {
    private Context mContext;
    private Button mQiangBtn;
    private String mActivityId;
    private EditText mFeiYongText;
    private EditText mBackUpEdit;
    private String mFeiyong;
    private int completeInfoTaskId = -1;
    private String mFacility = "";
    private String mBackUp;

    private RelativeLayout mCaipinLayout;
    private String selectCaipin;
    private String mCaipinString;
    private TextView mCaipinText;
    private CheckBox mFreeWifi;
    private CheckBox mFreePark;
    private CheckBox mFreeDaoBao;
    private CheckBox mFreeZiDai;

    private CheckBox mNoFreeWifi;
    private CheckBox mNoFreePark;
    private CheckBox mNoFreeDaoBao;
    private CheckBox mNoFreeZiDai;
    private CheckBox mJiuShuiZiFei;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qiangyuefan);
        initView();
        mContext = this;

    }

    private void initView() {
        Intent intent = getIntent();
        mActivityId = intent.getStringExtra("ACTIVITYID");
        mFeiYongText = (EditText) findViewById(R.id.hd_feiyong_content);
        mBackUpEdit = (EditText) findViewById(R.id.jianjie_content);
        mQiangBtn = (Button) findViewById(R.id.qiangdan_btn);
        mQiangBtn.setOnClickListener(this);
        mCaipinLayout = (RelativeLayout) findViewById(R.id.hd_caipin_layout);
        mCaipinText = (TextView) findViewById(R.id.hd_caipin_content);
        mCaipinLayout.setOnClickListener(this);
        mFreeWifi = (CheckBox) findViewById(R.id.wifi_btn);
        mFreePark = (CheckBox) findViewById(R.id.tingche_btn);
        mFreeDaoBao = (CheckBox) findViewById(R.id.dabao_btn);
        mFreeZiDai= (CheckBox) findViewById(R.id.zidai_btn);
        mNoFreeWifi= (CheckBox) findViewById(R.id.nowifi_btn);
        mNoFreePark= (CheckBox) findViewById(R.id.notingche_btn);
        mNoFreeDaoBao= (CheckBox) findViewById(R.id.nodabao_btn);
        mNoFreeZiDai= (CheckBox) findViewById(R.id.nozidai_btn);
        mJiuShuiZiFei= (CheckBox) findViewById(R.id.jiushuizifei);

    }

    private boolean checkInput() {
        mFeiyong = mFeiYongText.getText().toString();
        mFacility = "";
        if (mFreeWifi.isChecked()) {
            mFacility += "免费WIFI\n";
        }
        if (mFreePark.isChecked()) {
            mFacility += "免费停车\n";
        }
        if (mFreeDaoBao.isChecked()) {
            mFacility += "免费打包\n";
        }
        if (mFreeZiDai.isChecked()) {
            mFacility += "可自带酒水\n";
        }

        if (mNoFreeWifi.isChecked()) {
            mFacility += "无WIFI\n";
        }
        if (mNoFreePark.isChecked()) {
            mFacility += "无停车位\n";
        }
        if (mNoFreeDaoBao.isChecked()) {
            mFacility += "不可外带\n";
        }
        if (mNoFreeZiDai.isChecked()) {
            mFacility += "不可自带酒水\n";
        }
        if (mJiuShuiZiFei.isChecked()) {
            mFacility += "酒水自费\n";
        }
        int position = mFacility.lastIndexOf("\n");
        if (position > -1) {
            mFacility = mFacility.substring(0, position);
        }
//        PalLog.e("位置",position+"");
//        PalLog.e("设施",mFacility);
        if (TextUtils.isEmpty(mFeiyong)) {
            Toast.makeText(mContext, R.string.fee_null, Toast.LENGTH_SHORT)
                    .show();
            return false;
        } else if (ViewUtil.isNull(selectCaipin)) {
            Toast.makeText(mContext, "请选择菜品", Toast.LENGTH_SHORT)
                    .show();
        }
        return true;
    }

    private void doComplete() {
        mBackUp = mBackUpEdit.getText().toString();
        CompleteActivityInfoTask task = new CompleteActivityInfoTask(mActivityId, mFeiyong, selectCaipin, mFacility, mBackUp);
        task.addListener(completeListener);
        completeInfoTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener completeListener = new ShowToastTaskEventListener(this) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == completeInfoTaskId) {
                Intent qIntent = new Intent(mContext, ChuangJianChengGongActivity.class);
                qIntent.putExtra("FROM", "QIANG");
                startActivity(qIntent);
                finish();
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == completeInfoTaskId) {
                completeInfoTaskId = -1;
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.qiangdan_btn:

                if (checkInput()) {
                    doComplete();
                }

                break;

            case R.id.hd_caipin_layout:

                Intent cpInent = new Intent(mContext, CaiPinListActivity.class);
                cpInent.putExtra("FROM", 2);
                if (!ViewUtil.isNull(selectCaipin)) {
                    cpInent.putExtra("SELECT_CAIPIN", selectCaipin);
                }
                startActivityForResult(cpInent, 4);

                break;

        }

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 4) {
            if (data != null) {
                mCaipinString = data.getStringExtra("SELECT_ALREADY");
                selectCaipin = data.getStringExtra("SELECTCAIPIN");
                PalLog.e("选中的菜品", selectCaipin);
                mCaipinText.setText(mCaipinString);
            }
        }

    }
}
